<?php

session_start();
require 'connection.php';

$app = new \Slim\Slim();
$app->get('/oper_rep/pur/:date', authorize('admin'), 'rpt_pur');
$app->get('/oper_rep/pur_op/:id', authorize('admin'), 'rpt_pur_op');
$app->get('/oper_rep/sale/:date', authorize('admin'), 'rpt_sale');
$app->get('/oper_rep/sale_op/:id', authorize('admin'), 'rpt_sale_op');

$app->run();

function rpt_pur($date) {
    $sql_list = "SELECT h.number, h.serie, h.date, h.operator_id, h.total, 
                h.subtotal, h.tax , o.name, o.code FROM headers h
                INNER JOIN operators o ON o.id = h.operator_id
                WHERE h.type = 1 AND o.type = 1 AND month(date) = :date";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql_list);
        $stmt->bindParam("date", $date);
        $stmt->execute();
        $rst_find = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode($rst_find);
    } catch (PDOException $e) {
        echo '{"error":{"text":' . $e->getMessage() . '}}';
        return;
    }
}

function rpt_pur_op($id) {
   $sql_list = "SELECT h.number, h.serie, h.date, h.operator_id, h.total, h.subtotal, h.igv tax , o.name, o.code 
                FROM import_document h
                INNER JOIN operators o ON o.id = h.operator_id
                WHERE h.type = 1 AND o.type = 1 AND h.operator_id = :id";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql_list);
        $stmt->bindParam("id", $id);
        $stmt->execute();
        $rst_find = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode($rst_find);
    } catch (PDOException $e) {
        echo '{"error":{"text":' . $e->getMessage() . '}}';
        return;
    }
}

function rpt_sale($date) {
    $sql_list = "SELECT h.number, h.serie, h.date, h.operator_id, h.total, h.subtotal, h.igv tax , o.name, o.code 
                FROM import_document h
                INNER JOIN operators o ON o.id = h.operator_id
                WHERE h.type = 2 AND o.type = 2 AND month(date) = :date";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql_list);
        $stmt->bindParam("date", $date);
        $stmt->execute();
        $rst_find = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode($rst_find);
    } catch (PDOException $e) {
        echo '{"error":{"text":' . $e->getMessage() . '}}';
        return;
    }
}

function rpt_sale_op($id) {
   $sql_list = "SELECT h.number, h.serie, h.date, h.operator_id, h.total, h.subtotal, h.igv tax , o.name, o.code 
                FROM import_document h
                INNER JOIN operators o ON o.id = h.operator_id
                WHERE h.type = 2 AND o.type = 2 AND h.operator_id = :id";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql_list);
        $stmt->bindParam("id", $id);
        $stmt->execute();
        $rst_find = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode($rst_find);
    } catch (PDOException $e) {
        echo '{"error":{"text":' . $e->getMessage() . '}}';
        return;
    }
}



?>